package cn.fastoo.common.dao.order;

import cn.fastoo.common.dao.member.MemberDao;
import cn.fastoo.common.dao.member.MemberGuardianDao;
import cn.fastoo.common.entity.order.OrderBase;
import cn.fastoo.common.sampleframe.dao.FastDao;
import cn.fastoo.common.sampleframe.dao.impl.FastDaoImpl;
import cn.fastoo.common.sampleframe.search.Search;
import cn.fastoo.common.sampleframe.springside.Collections3;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;

@Component
@Transactional
public class OrderBaseDao extends FastDaoImpl<OrderBase> implements FastDao<OrderBase> {

    public Logger logger = LoggerFactory.getLogger(OrderBaseDao.class);

    @Autowired
    @Qualifier("mainDS")
    private void setJt(DataSource ds){
        this.jt = new JdbcTemplate(ds);
    }
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private MemberGuardianDao memberGuardianDao;

    public Page<OrderBase> findPage(Search search, Long userId) {
        Page<OrderBase> orders =  super.findPage(search);
        if(Collections3.isNotEmpty(orders.getContent())){
            /** 填充学生信息和监护人信息 **/
            orders.getContent().forEach(t->{
                t.setMemberName(memberDao.findById(t.getMemberId()).getUsername());
                t.setMemberGuardianName(memberGuardianDao.findById(t.getMemberGuardianId()).getGuardianName());
            });
        }
        return orders;
    }
}